package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.MarketAdminKeyWordService;
import com.cskaoyan.market.admin.service.impl.MarketAdminKeyWordServiceImpl;
import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.util.AdminResponseCode;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:TaoRencan
 * @Date: 2023/3/21 11:17
 */
@WebServlet("/admin/keyword/*")
public class AdminKeyWordController extends HttpServlet {
    //起手建立service 引用
    MarketAdminKeyWordService marketAdminKeyWordService = new MarketAdminKeyWordServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String function = requestURI.replace(request.getContextPath() + "/admin/keyword/", "");
        if ("create".equals(function)) {
            createFunction(request, response);
        }
        if ("update".equals(function)) {
            update(request, response);
        }
        if ("delete".equals(function)) {
            delete(request, response);
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtil.getRequestBody(request);
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        String id = JacksonUtil.parseString(requestBody, "id");
        String isDefault = JacksonUtil.parseString(requestBody, "isDefault");
        String isHot = JacksonUtil.parseString(requestBody, "isHot");
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        String url = JacksonUtil.parseString(requestBody, "url");
        if (StringUtils.isEmpty(keyword)) {
            response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        marketAdminKeyWordService.delete(addTime, id, isDefault, isHot, keyword, updateTime, url);
        response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));

    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtil.getRequestBody(request);
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        String id = JacksonUtil.parseString(requestBody, "id");
        String isDefault = JacksonUtil.parseString(requestBody, "isDefault");
        String isHot = JacksonUtil.parseString(requestBody, "isHot");
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        String url = JacksonUtil.parseString(requestBody, "url");
        if (StringUtils.isEmpty(keyword)) {
            response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        MarketKeyword marketKeyword = marketAdminKeyWordService.update(addTime, id, isDefault, isHot, keyword, updateTime, url);
        HashMap<Object, Object> data = getResboseBody(marketKeyword);
        response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(data)));
    }

    private void createFunction(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonCreate = HttpUtil.getRequestBody(request);
        String isDefault = JacksonUtil.parseString(jsonCreate, "isDefault");
        String keyword = JacksonUtil.parseString(jsonCreate, "keyword");
        String url = JacksonUtil.parseString(jsonCreate, "url");
        String isHot = JacksonUtil.parseString(jsonCreate, "isHot");
        //特注keyword 不为空
        if (StringUtils.isEmpty(keyword)) {
            response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail(AdminResponseCode.ADMIN_INVALID_NAME, "关键字不为空")));
            return;
        }
        if (url == null) {
            url = "";
        }
        MarketKeyword marketKeyword = marketAdminKeyWordService.create(isDefault, keyword, url, isHot);
        HashMap<Object, Object> data = getResboseBody(marketKeyword);
        response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(data)));

    }

    private HashMap<Object, Object> getResboseBody(MarketKeyword marketKeyword) {
        HashMap<Object, Object> data = new HashMap<>();
        data.put("keyword", marketKeyword.getKeyword());
        data.put("addTime", marketKeyword.getAddTime());
        data.put("id", marketKeyword.getId());
        data.put("isDefault", marketKeyword.getIsDefault());
        data.put("isHot", marketKeyword.getIsHot());
        data.put("updateTime", marketKeyword.getUpdateTime());
        data.put("url", marketKeyword.getUrl());
        return data;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String listFunction = requestURI.replace(request.getContextPath() + "/admin/keyword/", "");
        if ("list".equals(listFunction)) {
            listFunction(request, response);
        }
    }

    private void listFunction(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        String keyword = request.getParameter("keyword");
        String url = request.getParameter("url");
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        if (StringUtils.isEmpty(page) || StringUtils.isEmpty(limit)) {
            response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgumentValue()));
            return;
        }

        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        List<MarketKeyword> marketKeywordList = marketAdminKeyWordService.List(page, limit, keyword, url, sort, order);
        response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(marketKeywordList)));
    }
}
